<?php

function CreateChildMenu($menu, $parent_id) {
    $idChildMenu = array();
    $nameChildMenu = array();
    foreach ($menu as $row) {
        if ($row['PARENT_ID'] == $parent_id) {
            array_push($idChildMenu, $row["ID"]);
            array_push($nameChildMenu, $row["NAME"]);
        }
    }
    $dem = count($idChildMenu);   
    if ($dem > 0) {
        echo "<ul>";
        for ($i = 0; $i < $dem ; $i++) {
            echo "<li>";
            echo "<a href='".$idChildMenu[$i]."'>" . $nameChildMenu[$i] . "</a>";
            CreateChildMenu($menu, $idChildMenu[$i]);
        }
        echo"</ul>";
    }
    else
        echo "</li>";
}

function CreateMenu($menu) {
    echo "<nav>";
    echo "<ul>";
    foreach ($menu as $row) {
        if ($row["PARENT_ID"] == 0) {
            echo "<li>";
             echo "<a href=''>" . $row["NAME"] . "</a>";
             CreateChildMenu($menu, $row["ID"]);
        }
    }
}

CreateMenu($menus);
?>
